package com.oscar.ehome.utils.dowload.db;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

/**
 * Created by oscar
 * author: MengWanhe
 * date: On 2017/7/17
 */

public class VideoContentProvider extends ContentProvider {

    private SQLiteDatabase db;
    private final static int DB_VERSION = 2;

    @Override
    public boolean onCreate() {
        VideoOpenHelper bookOpenHelper = new VideoOpenHelper(this.getContext(), null, null, DB_VERSION);
        db = bookOpenHelper.getWritableDatabase();
        if (db == null)
            return true;
        else
            return false;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        return db.query(uri.getLastPathSegment(), projection, selection, selectionArgs, null, null, sortOrder);
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        long id = db.insert(uri.getLastPathSegment(), null, values);
        return ContentUris.withAppendedId(uri, id);
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
        return db.delete(uri.getLastPathSegment(), selection, selectionArgs);
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
        return db.update(uri.getLastPathSegment(), values, selection, selectionArgs);
    }
}
